package com.heima.article.listen;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.common.constants.message.NewsUpOrDownConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ArticleUpOrDownListener {

    @Autowired
    ApArticleConfigService apArticleConfigService;

    @RabbitListener(queuesToDeclare = @Queue(
            value = NewsUpOrDownConstants.NEWS_UP_FOR_ARTICLE_CONFIG_QUEUE
    ))
    public void newsUp(String articleId){
        log.info("接受到上架消息,文章id{}",articleId);
        //根据文章Id,查询当前的文章
        LambdaUpdateWrapper<ApArticleConfig> wrapper = Wrappers.<ApArticleConfig>lambdaUpdate();
        wrapper.eq(ApArticleConfig::getArticleId, articleId)
                .set(ApArticleConfig::getIsDown, 0);
        apArticleConfigService.update(wrapper);
    }


    @RabbitListener(queuesToDeclare = @Queue(
            value = NewsUpOrDownConstants.NEWS_DOWN_FOR_ARTICLE_CONFIG_QUEUE
    ))
    public void newsDOWN(String articleId){
        log.info("接受到下架消息,文章id{}",articleId);
        //根据文章Id,查询当前的文章
        LambdaUpdateWrapper<ApArticleConfig> wrapper = Wrappers.<ApArticleConfig>lambdaUpdate();
        wrapper.eq(ApArticleConfig::getArticleId, articleId)
                .set(ApArticleConfig::getIsDown, 1);
        apArticleConfigService.update(wrapper);
    }
}
